/***************************************************************************
 *   Copyright (C) 2007 by Ivan Vasić                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef CENTRALVIEW_H
#define CENTRALVIEW_H

#include <QtCore>
#include <QSortFilterProxyModel>

#include "ui_uicentralwidget.h"

class SqlObjectsModel;
class SettingsWidget;

/**
 * @author Ivan Vasić <ivasic@gmail.com>
*/
class CentralView : public QWidget, private Ui::CentralWidgetBase
{
		Q_OBJECT
	public:
		CentralView(QWidget *parent = 0);
		~CentralView();
		
		/**
		 * @brief Prikazuje torrent listu u listView.
		 * Postavlja m_torrentModel za aktivni model listView-a.
		 * @warning Poziva deleteModels() fju koja brise sve modele (objekte) sto mozda nije najsrecnije resenje
		 * ali u svakom slucaju oslobadja malo memorije + omogucava update/refresh modela tj ponavlja database query. Promeni ovo ako nadjes neko pametnije resenje.
		 */
		void showTorrents();
		
		/**
		 * @brief Prikazuje user listu u listView.
		 * Postavlja m_userModel za aktivni model listView-a.
		 * @warning Poziva deleteModels() fju koja brise sve modele (objekte) sto mozda nije najsrecnije resenje
		 * ali u svakom slucaju oslobadja malo memorije + omogucava update/refresh modela tj ponavlja database query. Promeni ovo ako nadjes neko pametnije resenje.
		 */
		void showUsers();
		
		/**
		 * @brief Prikazuje SettingsWidget kao aktivni widget iz stackedWidgets.
		 */
		void showSettings();
		
		
		///Prikazuje stats widget
		void showStats();
		
		/**
		 * @brief Brise sve modele (objekte)
		 * m_torrentModel i m_userModel
		 */
		void deleteModels();
		
	public slots:
		
		///Radi update trenutno prikazane liste. Ako je model u pitanju, odradi query i prikaze nove rezultate.
		void refreshCurrentView();		
		
		///Radi refresh labela sa statistikama (donji desni ugao CentralView-a)
		void updateLabels();
		
		/**
		 * @brief Prikazuje HTML properties za selektovani objekat.
		 * Za Torrent/User prikazuje HTML properties u propertiesView-u.
		 */
		void itemClicked(const QModelIndex& index);
		
		/**
		 * @brief Prikazuje HTML properties u propertiesView-u.
		 * Properties se prikazuju za selektovani objekat i to po <i>identifikatorskom polju u bazi!</i>. Polje mora da bude UNIQUE (npr. 'id') i odredjuje se u konkretnom modelu za konkretnu bazu podataka. Odavde se samo prosledjuje <i>vrednost</i> tog polja i po njemu se vrsi SELECT u modelu.
		 * @param relFieldValue - relevant field value. UNIQUE polje (vrednost polja) po kome se identifikuje objekat.
		 */
		void showHtmlProperties(const QString& relFieldValue);
		
		/**
		 * @brief Brise selektovani (u listView) objekat iz baze.
		 */
		void removeItem();
		
		/**
		 * @brief Exports a torrent to a file for specific user.
		 */
		void slotExportTorrent();
		
		///Azurira broj aktivnih TCP konekcija
		void numConnectionsChanged(int num_connections);
		
	private slots:		
		void clearPropertiesView();
		void listViewContextMenuRequested(const QPoint& pos);
		void showTorrentPropertiesDialog();
		void showUserPropertiesDialog();
		
		void listViewDoubleClicked(QModelIndex);
		
	private:
		
		QString constructHtmlProperties(QStringList& properties);
		
		void setupActions();
		void setupConnections();
		
		///Kreira i dodaje extra widgete (m_settingsWidget, stats...)
		void setupWidgets();
		
	private:
		/*TorrentModel* m_torrentModel;
		UserModel* m_userModel;		
		ObjectModel* m_currentModel;
		QSortFilterProxyModel* m_filterModel;*/
		
		SettingsWidget* m_settingsWidget;
		
		SqlObjectsModel* m_torrents;
		SqlObjectsModel* m_users;
		SqlObjectsModel* m_currentModel;
		QSortFilterProxyModel* m_filterModel;
		
		QAction* actionRefresh;
		QAction* actionRemoveItem;
		QAction* actionExportTorrent;
		QAction* actionShowTorrentPropertiesDialog;
		QAction* actionShowUserPropertiesDialog;
};

#endif
